home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / minigl.lha / MiniGL / demos / varray.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-01  |  4.1 KB  |  213 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #ifdef __VBCC__
  5. #include <extra.h>    //OF
  6. #endif
  7.  
  8. #include <mgl/gl.h>
  9.  
  10. //#include <StAnDebug.h>
  11.  
  12. #ifdef __VBCC__
  13. #pragma amiga-align
  14. #endif
  15.  
  16. #include <proto/dos.h>
  17.  
  18. #ifdef __VBCC__
  19. #pragma default-align
  20. #endif
  21.  
  22. GLenum LockMode = MGL_LOCK_MANUAL;
  23.  
  24.  
  25. typedef struct myVertex_t
  26. {
  27.     GLfloat x, y, z;
  28.     GLubyte r, g, b, a;
  29.     GLfloat s, t;
  30. } myVertex;
  31.  
  32.  
  33. myVertex cube[] =
  34. {
  35.   { -1.0, -1.0, -1.0,  0, 0, 255, 255,  0.5, 0.5 }, // 0
  36.   { -1.0, -1.0,  1.0,  0, 0, 255, 255,  0.5, 0.0 }, // 1
  37.   { -1.0,  1.0,  1.0,  0, 255, 255, 255,  0.0, 0.0 }, // 2
  38.   { -1.0,  1.0, -1.0,  0, 255, 255, 255,  0.0, 0.5 }, // 3
  39.   {  1.0,  1.0,  1.0,  255, 255, 255, 255,  0.5, 0.5 }, // 4
  40.   {  1.0, -1.0,  1.0,  255, 0, 255, 255,  0.5, 0.0 }, // 5
  41.   {  1.0, -1.0, -1.0,  255, 0, 255, 255,  0.0, 0.0 }, // 6
  42.   {  1.0,  1.0, -1.0,  255, 255, 255, 255,  0.5, 0.5 } // 7
  43. };
  44.  
  45.  
  46. unsigned char cube_idx[] =
  47. {
  48.     1, 4, 5,  1, 2, 4,      // Front
  49.     5, 6, 7,  5, 4, 7,      // Right
  50.     6, 3, 0,  6, 7, 3,      // Back
  51.     2, 3, 0,  0, 1, 2,      // Left
  52.     2, 4, 3,  2, 4, 7,      // Top
  53.     1, 5, 6,  1, 6, 0       // Bottom
  54. };
  55.  
  56. /*
  57. note that triangle 3 and triangle 8 can be draw with glDrawArrays - this is exploited with -compiled parameter
  58. to demonstrate the flexibility of compiled arrays
  59. */
  60.  
  61. #define NUMIDX sizeof(cube_idx)/sizeof(unsigned char)
  62.  
  63.  
  64. int main(int argc, char *argv[])
  65. {
  66.     int i;
  67.     GLuint t = 0;
  68.     GLboolean Compiled_Arrays;
  69.     GLboolean No_Pipeline;
  70.  
  71.     Compiled_Arrays = GL_FALSE;
  72.     No_Pipeline = GL_FALSE;
  73.  
  74.     MGLInit();
  75.  
  76.     mglChooseVertexBufferSize(1024);
  77.     mglChooseNumberOfBuffers(2);
  78.     mglChoosePixelDepth(16);
  79.     mglChooseWindowMode(GL_TRUE);
  80.  
  81.     for (i=1; i<argc; i++)
  82.     {
  83.          if (0 == strcmp(argv[i], "-lock"))
  84.          {
  85.  
  86.        if( (i+1)<argc )    //OF
  87.        {
  88.        i++;
  89.  
  90.             if (0 == stricmp(argv[i], "auto"))
  91.             {
  92.                 LockMode = MGL_LOCK_AUTOMATIC;
  93.             }
  94.             else if (0 == stricmp(argv[i], "smart"))
  95.             {
  96.                 LockMode = MGL_LOCK_SMART;
  97.             }
  98.             else
  99.             {
  100.                 LockMode = MGL_LOCK_MANUAL;
  101.             }
  102.        }
  103.  
  104.        }
  105.  
  106.        if (0 == stricmp(argv[i], "-compiled"))
  107.        {
  108.        //i++;    //OF (removed)
  109.             Compiled_Arrays = GL_TRUE;
  110.        }
  111.  
  112.        if (0 == stricmp(argv[i], "-bypass"))
  113.        {
  114.        //i++;    //OF (removed)
  115.             No_Pipeline = GL_TRUE;
  116.        }
  117.  
  118.     }
  119.  
  120.     if(Compiled_Arrays == GL_TRUE && No_Pipeline == GL_FALSE)
  121.         printf("Using compiled vertexarrays..\n");
  122.     else
  123.         printf("Using standard vertexarrays..\n");
  124.  
  125.     if(No_Pipeline == GL_TRUE)
  126.         printf("Bypassing transformation pipeline..\n");
  127.  
  128.  
  129.     if (mglCreateContext(0, 0, 640, 480))
  130.     {
  131.         mglLockMode(LockMode);    //OF (was called before mglCreateContext())
  132.  
  133.         if (LockMode == MGL_LOCK_MANUAL)
  134.             mglLockDisplay();
  135.  
  136.         glViewport(0, 0, 640, 480);
  137.  
  138.         glDisable(GL_CULL_FACE);
  139.  
  140.         glMatrixMode(GL_PROJECTION);
  141.         glLoadIdentity();
  142.         gluPerspective(90.0, 1.333333, 0.2, 100.0);
  143.  
  144.         glMatrixMode(GL_MODELVIEW);
  145.         glTranslatef(0.0, 0.0, -8.0);
  146.  
  147.         glRotatef(30.0, 1.0, 1.0, 1.0);
  148.  
  149.         glClearColor(0.0, 0.0, 0.0, 1.0);
  150.         glClear(GL_COLOR_BUFFER_BIT);
  151.  
  152.         if(No_Pipeline == GL_TRUE)
  153.         {
  154.             glDisable(MGL_ARRAY_TRANSFORMATIONS);
  155.         }
  156.  
  157.         glEnableClientState(GL_COLOR_ARRAY);
  158.         glEnableClientState(GL_VERTEX_ARRAY);
  159.  
  160.         //PUTS( "b4 pointers" );
  161.  
  162.         glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(myVertex), (void *)&cube[0].r);
  163.  
  164.         glVertexPointer(3, GL_FLOAT, sizeof(myVertex), (void *)cube);
  165.  
  166.         if(Compiled_Arrays == GL_TRUE &&     No_Pipeline == GL_FALSE)
  167.         {
  168.  
  169.         glLockArrays(0, 8); //preprocess data
  170.  
  171. /*
  172.     this could be done with a single glDrawElements call,
  173.     but that is not what this part of the demo is about :)
  174. */
  175.  
  176.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, (void *)cube_idx);
  177.  
  178.         glDrawArrays(GL_TRIANGLES, 5, 3);
  179.  
  180.         glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, (void *)&cube_idx[9]);
  181.  
  182.         glDrawArrays(GL_TRIANGLES, 0, 3);
  183.  
  184.             glBegin(GL_TRIANGLES);
  185.  
  186.             for (i=24; i<NUMIDX; i++)
  187.             glArrayElement(cube_idx[i]);
  188.  
  189.             glEnd();
  190.  
  191.  
  192.         glUnlockArrays();
  193.  
  194.         }
  195.         else
  196.         {
  197.             glDrawElements(GL_TRIANGLES, NUMIDX, GL_UNSIGNED_BYTE, (void *)cube_idx);
  198.         }
  199.  
  200.     glDisableClientState(GL_COLOR_ARRAY);
  201.     glDisableClientState(GL_VERTEX_ARRAY);
  202.  
  203.     mglSwitchDisplay();
  204.  
  205.     Delay(50);
  206.  
  207.     mglDeleteContext();
  208.     }
  209.  
  210.     MGLTerm();
  211.     return 0;
  212. }
  213.